//
//  JsRequestModel.m
//
//  Created by GikkiAres on 2020/2/27.
//

#import "JsRequestConfig.h"
#import "JsYy_Model.h"

@interface JsRequestConfig()
{
	JsRequestMethod _method;
}

@end

@implementation JsRequestConfig


- (instancetype)initWithMethod:(JsRequestMethod)method requestBodyInputType:(JsRequestBodyInputType)requestBodyInputType responseBodyOutputType:(JsResponseBodyOutputType)responseOutputType {
	self = [super init];
	if (self) {
		_method = method;
		_requestBodyInputType = requestBodyInputType;
		_responseBodyOutputType = responseOutputType;
		
		[self commonInit];
	}
	return self;
}

- (void) commonInit {
	
}
#pragma mark 2 Interface
- (JsRequestMethod)requestMethod {
	return _method;
}

/*获取实际请求的urlString
 a,如果手动设置了interfaceUrlString,则返回此string.
 b,没有_urlParameter,则返回 serverAddress/interfacePath
 c,有_urlParameter,则返回 serverAddress/interfacePath?key1=value1&key2=value2
*/
- (NSString *)interfaceUrlString {
	if(_interfaceUrlString) {
		return _interfaceUrlString;
	}
	else {
		return  [NSString stringWithFormat:@"%@/%@",self.serverAddress,self.interfacePath];
	}
}


- (NSString *)requestUrlString {
	if(_requestUrlString) {
		return _requestUrlString;
	}
	else {
			if(!_urlParameter) {
				return [self interfaceUrlString];
			}
			else {
					//请求的url需要添加请求参数.
				NSString * postAttachUrlParameter = @"";
				if(_urlParameter.allKeys.count) {
					NSMutableString * mstr = [[NSMutableString alloc]initWithString:@"?"];
					__block int index = 0;
					[_urlParameter enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
						if(index!=0) {
							[mstr appendString:@"&"];
						}
						[mstr appendFormat:@"%@=%@",key,obj];
						index++;
					}];
					postAttachUrlParameter = mstr;
				}
				return [NSString stringWithFormat:@"%@%@",[self interfaceUrlString],postAttachUrlParameter];
			}
	}
}

#pragma mark 3 InnerFunc

- (NSString *)requestMethodName {
	switch(_method) {
		case JsRequestMethodGet:
			return @"GET";
		case JsRequestMethodPost:
			return @"POST";
		case JsRequestMethodHead:
			return @"HEAD";
		case JsRequestMethodOptions:
			return @"OPTIONS";
		case JsRequestMethodPut:
			return @"PUT";
		case JsRequestMethodPatch:
			return @"PATCH";
		case JsRequestMethodDelete:
			return @"DELETE";
		case JsRequestMethodTrace:
			return @"TRACE";
		case JsRequestMethodConnect:
			return @"CONNECT";
	}
}

-(NSString *)requestBodyInputString {
	switch(_requestBodyInputType) {
		case JsRequestBodyInputTypeNone:
			return @"None";
		case JsRequestBodyInputTypeData:
			return @"Data";
		case JsRequestBodyInputTypeMapToJson:{
			NSDictionary * dic = (NSDictionary *)_requestBodyInput;
			NSString * json = [dic jsyy_modelToJSONString];
			return json;
		}
		case JsRequestBodyInputTypeMapToFormdata:
		case JsRequestBodyInputTypeMapToUrlEncoded:
			return @"Todo";
	}
}



- (NSDictionary<NSString *,NSString *> *)requestHeader {
	if(!_requestHeader) {
		_requestHeader = [NSMutableDictionary dictionary];
	}
	return _requestHeader;
}
- (NSMutableDictionary *)mutableRequestHeader {
	return (NSMutableDictionary *)[self requestHeader];
}

- (void)setValue:(NSString *)value forRequestHeaderKey:(NSString *)key {
	NSMutableDictionary * mDic = [self mutableRequestHeader];
	[mDic setObject:value forKey:key];
}
- (void)addDicForRequestHeader:(NSDictionary *)dic {
	[[self mutableRequestHeader] addEntriesFromDictionary:dic];
}


@end
